// injected.js
(function () {
  const injectUrl = {
    url1: 'api/v4/pdp/get_pc',
    url3: 'api/v4/pdp/hot_sales/get',
    url2: 'mtop.1688.shop.data.get/',
  };

  const originalXHROpen = XMLHttpRequest.prototype.open;
  const originalFetch = window.fetch;
  console.log("开始过滤========")

  // Hook into XMLHttpRequest
  XMLHttpRequest.prototype.open = function () {
    this.addEventListener('load', function () {
      if (this.responseURL.includes(injectUrl.url1) || this.responseURL.includes(injectUrl.url2) || this.responseURL.includes(injectUrl.url3)) {
        console.log('XHR response for ' + this.responseURL + ': ', this.responseText);
      }
    });
    return originalXHROpen.apply(this, arguments);
  };

  // Hook into fetch
  window.fetch = function () {
    return originalFetch.apply(this, arguments)
      .then(response => {
        // console.log('====' + response.url)
        //后续要写一些过滤器
        if (response.url.includes(injectUrl.url1) || response.url.includes(injectUrl.url2) || response.url.includes(injectUrl.url3)) {
          response.clone().text() // Clone the response stream
            .then(text => {
              // console.log('Fetch response for ' + response.url + ': ', text);
              window.postMessage({ type: "FROM_PAGE", url: response.url, text: text }, "*");
            });
        }
        return response;
      });
  };
})();
